package com.itheima.a09threadsafe1;

public class MyThread extends Thread {

    static int ticket = 0;

    //锁对象一定是唯一的 -- 信号量
    static final Object lock = new Object();

    public void run() {


        while (true) {
            synchronized (lock) {
                //父类的 run 方法没有抛异常 子类重写的也不能
                try {
                    Thread.sleep(100)
                    ;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (ticket < 100) {
                    ticket++;
                    System.out.println(getName() + "正在卖第：" + ticket + "张票");
                } else {
                    break;
                }
            }
        }
    }
}
